package com.ruoyi.StarStudio.Controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.StarStudio.Service.TreeService;
import com.ruoyi.StarStudio.Utils.QueryPageUtils;
import com.ruoyi.StarStudio.Domain.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/trees")
public class TreeController {

    @Autowired
    private TreeService treeService;

    //获取所有果树信息
    @GetMapping
    public Result getAll(){
        List<Tree> treeList = null;
        try {
            treeList = treeService.getAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.TREE_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.TREE_QUERY_SUCCESS, treeList);
    }

    /**
     * 导出果树数据到 Excel表格
     * @param response
     * @param tree
     */
    @Log(title = "果树数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tree tree) {
        List<Tree> list = treeService.getList(tree);
        ExcelUtil<Tree> util = new ExcelUtil<Tree>(Tree.class);
        util.exportExcel(response, list, "果树数据");
    }

    /**
     * 从 Excel表格导入果树数据
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "果树数据", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Tree> util = new ExcelUtil<Tree>(Tree.class);
        List<Tree> treeList = util.importExcel(file.getInputStream());
        String message = treeService.importTrees(treeList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 导入果树数据 Excel模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Tree> util = new ExcelUtil<Tree>(Tree.class);
        util.importTemplateExcel(response, "果树数据");
    }

    //分页搜索获取果树信息
    @GetMapping("/page")
    public PageResult getList(Tree tree){
        QueryPageUtils.startPage();
        List<Tree> treeList = null;
        PageInfo pageInfo;
        try {
            treeList = treeService.getList(tree);
            pageInfo = new PageInfo<>(treeList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.TREE_QUERY_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS, pageInfo.getTotal(), MessageConstant.TREE_QUERY_SUCCESS, treeList);
    }

    //根据地块id查询所有果树信息
    @GetMapping("/{zone_id}")
    public Result getAllByZoneId(@PathVariable Integer zone_id){
        List<Tree> treeList = null;
        try {
            treeList = treeService.getAllByZoneId(zone_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.TREE_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.TREE_QUERY_SUCCESS, treeList);
    }

    //分页获取某个地块的所有果树数据
    @GetMapping("/page/{zone_id}")
    public PageResult getPageByZoneId(@PathVariable Integer zone_id){
        QueryPageUtils.startPage();
        List<Tree> treeList = null;
        PageInfo pageInfo;
        try {
            treeList = treeService.getAllByZoneId(zone_id);
            pageInfo = new PageInfo<>(treeList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.TREE_QUERY_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS, pageInfo.getTotal(), MessageConstant.TREE_QUERY_SUCCESS, treeList);
    }

    //根据果树id查询果树信息
    @GetMapping("/one/{tree_id}")
    public Result getById(@PathVariable Integer tree_id){
        Tree treeById = null;
        try {
            treeById = treeService.getById(tree_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.TREE_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.TREE_QUERY_SUCCESS, treeById);
    }

    //添加果树信息
    @PostMapping("/{zone_id}")
    public Result add(@RequestBody Tree tree, @PathVariable Integer zone_id) {
        try {
            tree.setTree_zoneid(zone_id);
            //获取当前时间戳
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            tree.setTree_fertilizertime(timestamp);
            tree.setTree_pesticidetime(timestamp);
            treeService.add(tree);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.TREE_ADD_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.TREE_ADD_SUCCESS);
    }

    //修改果树信息
    @PutMapping
    public Result update(@RequestBody Tree tree){
        try {
            //获取当前时间戳
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            tree.setTree_fertilizertime(timestamp);
            tree.setTree_pesticidetime(timestamp);
            treeService.update(tree);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.TREE_UPDATE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.TREE_UPDATE_SUCCESS);
    }

    //根据果树id删除果树信息
    @DeleteMapping("/{tree_id}")
    public Result delete(@PathVariable Integer tree_id){
        try {
            treeService.delete(tree_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.TREE_DELETE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.TREE_DELETE_SUCCESS);
    }

}